<template>
  <el-container>
    <!-- 左侧菜单栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'">
      <div class="logo">
        <img src="../assets/logo.png" alt />
        <span v-show="!isCollapse">Chalk</span>
      </div>
      <el-menu
        :default-active="$route.path"
        :collapse="isCollapse"
        router
        unique-opened
        :collapse-transition="false"
        background-color="transparent"
        text-color="#fff"
        active-text-color="#f39800"
      >
        <!-- 代码开始 -->
        <template v-for="item in menulist">
          <!-- 子级菜单 -->
          <el-submenu
            :index="'/' + item.address"
            v-if="item.children && item.children.length > 0"
            :key="item.id"
          >
            <template slot="title">
              <i class="el-icon-star-off"></i>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="subItem in item.children">
              <el-menu-item
                :key="subItem.id"
                :index="'/' + item.address + '/' + subItem.address"
              >
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{ subItem.name }}</span>
                </template>
              </el-menu-item>
            </template>
          </el-submenu>
          <!-- 一级菜单 -->
          <el-menu-item
            v-else
            :index="'/' + item.address"
            :key="item.id"
          >
            <i class="el-icon-star-off"></i>
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
        <!-- 代码结束 -->
      </el-menu>
    </el-aside>
    <!-- 左侧菜单栏End -->

    <el-container>
      <el-header>
        <!-- 右侧面包屑 -->
        <div class="menu-on">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-button @click="menuclick" class="col">
              <i class="el-icon-s-operation"></i>
            </el-button>
            <el-button @click="theme" class="col">
              主题切换
            </el-button>
            <!-- <el-breadcrumb-item to="/home">
              <el-button @click="saveNavState('home')">首页</el-button>
            </el-breadcrumb-item> -->
            <el-breadcrumb-item
              v-for="(item, index) in breadList"
              :key="index"
              :to="item.path"
              @click="saveNavState(item.path)"
            >
              <el-button>
                {{ item.meta.title }}
              </el-button>
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <!-- 右侧面包屑End -->
        <div class="user">
          <img src="../assets/logo.png" alt />
        </div>
      </el-header>
      <!-- 右侧主体区域 -->
      <el-main>
        <!-- 返回顶部按钮:下拉才显示 -->
        <el-backtop target=".el-main"></el-backtop>
        <!-- 返回顶部按钮End -->

        <!-- 右侧视图区 -->
        <router-view></router-view>
        <!-- 右侧视图区End -->

        <!-- 右侧页脚区域 -->
          <el-footer> footer </el-footer>
        <!-- 右侧页脚区域End -->
      </el-main>
      <!-- 右侧主体区域End -->
    </el-container>
  </el-container>
</template>

<script>
import { getMenuList } from '../api/manage.js'
export default {
  data() {
    return {
      isCollapse: false,
      menulist: [],
      activePath: '',
      // 面包屑
      breadList: []
    }
  },
  // 页面加载前的事件
  created() {
    // 执行事件Mneu
    this.menu()
    this.bread_crumb()
    // 获得存储在网页的Session
    // this.activePath = window.sessionStorage.getItem('activePath')
    // console.log(this.activePath)
  },
  methods: {
    // 获取菜单栏
    async menu() {
      const res = await getMenuList()
      this.menulist = res.data
      // console.log(res)
      // console.log(this.$route)
    },
    // saveNavState(activePath) {
    //   // 存储当前页面的Session
    //   window.sessionStorage.setItem('activePath', '/' + activePath)
    //   this.activePath = activePath
    // },
    // 左侧导航栏伸缩
    menuclick() {
      this.isCollapse = !this.isCollapse
    },
    theme() {
      const root = document.querySelector('html')
      root.classList.add('theme-fourth')
    },
    // 面包屑
    bread_crumb() {
      const matched = this.$route.matched.slice(1)
      this.breadList = matched
    }
  },
  watch: {
    $route() {
      this.bread_crumb()
    }
  },
  computed: {
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    position: relative;
    line-height: 60px;
    text-align: center;
    background: var(--bg-color);
    img {
      vertical-align: middle;
      max-height: 100%;
    }
    .user {
      position: absolute;
      right: 0;
      top: 0;
      height: 100%;
      img {
        height: 80%;
        border: 2px solid #fff;
        border-radius: 50%;
        margin: 0 10px;
      }
    }
    .menu-on {
      position: relative;
      font-size: 20px;
      .el-breadcrumb {
        padding: 10px;
        .col {
          float: left;
          margin-right: 10px;
        }
      }
    }
  }
  .el-aside {
    transition: 0.5s;
    background: url(../assets/img/menu_bgc.jpg) no-repeat center / cover;
    .logo {
      padding: 10px;
      white-space: nowrap;
      img {
        display: inline-block;
        vertical-align: middle;
        max-height: 60px;
      }
      span {
        display: inline-block;
        vertical-align: middle;
        font-size: 24px;
        color: #19a84e;
      }
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #f5f5f5;
    padding: 15px;
  }
  .el-footer {
    height: 40px !important;
    line-height: 40px;
    font-size: 16px;
    text-align: center;
    color: #000;
    background-color: #eee;
    margin-top: 15px;
  }
}
</style>
